<template>
  <div>
    <div class="form">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="账号">
          <el-input v-model="formInline.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="formInline.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="FUNC_connect">连接</el-button>
          <el-button type="primary" @click="FUNC_getDevList">获取设备列表</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div v-for="(item, index) in devices" :key="index">
      <div>{{ item.name }}----在线状态：{{item.data.status==1?'在线':'离线'}}</div>
      <video :id="'remoteVideo'+item.data.deviceId" controls autoplay muted></video>
      <audio :id="'remoteAudio'+item.data.deviceId" controls autoplay></audio>
      <el-button type="primary" @click="open_video(item.data.deviceId,null)">打开实时视频</el-button>
      <el-button type="primary" @click="close_video(item.data.deviceId,null)">关闭实时视频</el-button>
      <el-button type="primary" @click="open_audio(item.data.deviceId,null)">打开声音监听</el-button>
      <el-button type="primary" @click="close_audio(item.data.deviceId,null)">关闭声音监听</el-button>
      <el-button type="primary" @click="p2p_video(item.data.deviceId)">双向视频</el-button>
      <el-button type="primary" @click="call(item.data.deviceId)">语音通话</el-button>
      <el-button type="primary" @click="getGPSList(item.data.deviceId)">获取历史位置</el-button>
    </div> -->
    <div class="flex-view">
      <div class="flex-1">
        <div class="flex-1-inner">
          <!-- 视频通话 -->
          <div class="yyth-view">
            <div class="spth-title">
              <span>视频通话</span>
              <span class="deviceNm">{{ deviceId }}</span>
            </div>
            <video :id="'remoteVideo'+deviceId" controls autoplay muted></video>
            <audio :id="'remoteAudio'+deviceId" controls autoplay></audio>
            <div class="action-btn" @click="p2p_video(deviceId)">开启通话</div>
            <!-- <div class="action-btn" @click="connect">连接网关</div>
            <div class="action-btn" @click="closeCall">关闭通话</div> -->
          </div>
        </div>
      </div>
      <div style="display:none">
        <video id="localVideo" autoplay controls></video>
        <audio id="localAudio" autoplay controls></audio>
      </div>
    </div>
    <div style="display:none">
      <video id="localVideo" autoplay controls></video>
      <audio id="localAudio" autoplay controls></audio>
    </div>
  </div>
</template>

<script>
import MCS8Utitl from "./js/MCS8Utitl";
import { mapGetters } from "vuex";
export default {
  mounted() {},
  data() {
    return {
      formInline: {
        username: "MKW",
        password: "mcs8@666"
      },
      devices: [],
      deviceId:"test061801"
    };
  },
  computed: {
    ...mapGetters(["anyDeviceGPS", "anyDeviceStatus"])
  },
  watch: {
    anyDeviceGPS(val) {
      console.log("anyDeviceGPS", val);
    },
    anyDeviceStatus(val) {
      console.log("anyDeviceStatus", val);
      val.forEach(item => {
        this.devices.forEach(item2 => {
          if (item.DevId == item2.data.deviceId) {
            item2.data.status = item.Status;
          }
        });
      });
    }
  },
  methods: {
    /**
     * 连接网关
     */
    FUNC_connect() {
      // let sdkclient = getSdkClient()
      if (MCS8Utitl.sdkclient.mConnected) {
        // console.log("sdkClient", this.sdkClient.mConnected);
        MCS8Utitl.sdkclient.close();
        alert("已断开连接");
      } else {
        // console.log("sdkClient", this.sdkClient.mConnected);
        // 连接成功回调
        MCS8Utitl.sdkclient
          .connect(
            this.formInline.username,
            this.formInline.password,
            document.getElementById("localVideo"),
            document.getElementById("localAudio")
          )
          .then(res => {
            if (res) {
              alert("连接成功");
            } else {
              alert("连接失败");
            }
          });
      }
    },
    // 获取设备列表
    FUNC_getDevList() {
      MCS8Utitl.FUNC_init_getDevList().then(res => {
        console.log("res", res);
        res.forEach(item => {
          if (item.type == 3) {
            this.devices.push(item);
          }
        });
      });
      MCS8Utitl.FUNC_init_getDevOnlineList();
    },
    // 打开实时视频
    open_video(devId, channelId) {
      MCS8Utitl.FUNC_openVideo(devId, channelId);
    },
    // 关闭实时视频
    close_video(devId, channelId) {
      MCS8Utitl.FUNC_closeVideo(devId, channelId);
    },
    // 打开声音监听
    open_audio(devId, channelId) {
      MCS8Utitl.FUNC_openAudio(devId, channelId);
    },
    // 关闭声音监听
    close_audio(devId, channelId) {
      MCS8Utitl.FUNC_closeAudio(devId, channelId);
    },
    // 双向视频
    p2p_video(devId) {
      MCS8Utitl.FUNC_CreateP2pMediaGroup(devId);
    },
    call(devId) {
      MCS8Utitl.FUNC_CreateP2pMediaGroup6(devId);
    },
    // 获取历史位置
    getGPSList(devId) {
      MCS8Utitl.FUNC_GPS_LIST({
        page: 1,
        pagesize: 10,
        devId: devId, // 设备ID
        st: "2024-07-01", // 开始时间
        et: "2024-07-11" // 结束时间
      }).then(res => {
        console.log(res);
      });
    }
  }
};
</script>

<style scoped lang="scss">
</style>
